package com.ly.system.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.system.system.bean.entity.SysPost;
import com.ly.system.system.bean.entity.SysUserPosition;
import com.ly.system.mapper.SysPostMapper;
import com.ly.system.service.ISysPostService;
import com.ly.system.service.ISysUserPositionService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 职务表
 *
 * @author jeecg
 * @Date: 2019-09-19
 */
@Service
@RequiredArgsConstructor
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {

    private final ISysUserPositionService userPositionService;

    @Override
    public R getId(String id) {
        SysPost entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(SysPost entity) {
        List<SysPost> list = this.list(
                new LambdaQueryWrapper<SysPost>()
        );
        return R.ok(list);
    }

    @Override
    public R getPage(SysPost entity, Page<SysPost> page) {
        IPage<SysPost> pageList = this.page(
                page,
                new LambdaQueryWrapper<SysPost>()
                        .like(StrUtil.isNotBlank(entity.getName()), SysPost::getName, entity.getName())
                        .orderByDesc(SysPost::getCreateTime)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(SysPost entity) {
        if (StrUtil.isEmpty(entity.getCode())) {
            // 生成职位编码10位
            entity.setCode(IdUtil.getSnowflakeNextIdStr());
        }
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(SysPost entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R delete(String id) {
        boolean flag = this.removeById(id);
        userPositionService.remove(
                new LambdaQueryWrapper<SysUserPosition>()
                        .eq(SysUserPosition::getPositionId,id)
        );
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<SysPost> dto) {
        List<SysPost> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public List<SysPost> getPositionList(String userId) {
        return this.baseMapper.getPositionList(userId);
    }
}
